<?php
/**
* 作用：关注操作-粉丝
* 作者: Jasmine@soe-soe.com
* 日期: 2016/5/12 11:47
* 公司: 浙江宣逸网络科技有限公司<www.soe-soe.com>
*/
namespace Common\Model;
use Think\Cache\Driver\Db;

class FollowModel extends BaseModel{

    /**
     * 增加/取消关注操作
     * @param  flag 关注状态标签，0：+关注；1：我关注他；2：他关注我；
     */
    public static function followOP($uid,$fid){
        $dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
        $dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');
        if(!$uid){
            $dataFailure['msg']="请登录后再操作";
            return $dataFailure;
        }

        $friendM = M('friendship',null,DB_MAIN_CFG);
        $where = "uid = {$uid} and fid ={$fid}";          //我关注了他

        if($friendM->where($where)->getField('status')=='1'){
            $data['status']=0;
            $data['updatetime']= time();
            $update=$friendM->where($where)->save($data);
            if($update===false){
                $dataFailure['msg']="操作失败";
                return $dataFailure;
            }

        }
        else if($friendM->where($where)->getField('status')=='0'){
            $data['status']=1;
            $data['updatetime']= time();
            $update=$friendM->where($where)->save($data);
            if($update===false){
                $dataFailure['msg']="操作失败";
                return $dataFailure;
            }
        }
        else{
            $data['uid'] = $uid;
            $data['fid'] = $fid;
            $data['createtime'] = time();
            $data['status'] = 1;

            $add = $friendM->add($data);
            if($add===false){
                $dataFailure['msg']="关注失败";
                return $dataFailure;
            }
        }

        return $dataSuccess;

    }

    /**
     * 我的粉丝和我的关注
     * @param $uid 关注操作的人
     * @param $fid 被关注的人
     * @param $tag 0：我的关注;1:我的粉丝.
     */
    public static function getMyFriendship ($uid,$tag,$page,$pagesize){
//        $shipcache = md5($uid.$tag.$page.$pagesize);(需要加分隔符隔开)
//        $dataSuccess = S($shipcache);
//        if($dataSuccess){
//            return $dataSuccess;
//        }

        $dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
        $dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');
        if(!$uid){
            $dataFailure['msg']="请登录后再操作";
            return $dataFailure;
        }

        $dataSuccess['page'] = $page ? $page :1;          //返回当前页
        $offset = ($dataSuccess['page']-1)*$pagesize;     //起始条数

        $friendM = M('friendship',null,DB_MAIN_CFG);
        if($tag){
            //获取粉丝列表数据
            $dataList=$friendM->where("fid={$uid} and status=1")->limit($offset,$pagesize)->select();
            if($dataList===false){
                $dataFailure['msg']="数据获取失败";
                return $dataFailure;
            }
        }else{
            //获取关注列表数据
            $dataList =$friendM->where("uid={$uid} and status=1")->limit($offset,$pagesize)->select();
            if($dataList===false){
                $dataFailure['msg']="数据获取失败";
                return $dataFailure;
            }
        }
        //获取我的粉丝或关注的人 发表文章数和粉丝数、关注数
        foreach($dataList as $key => $val) {
            if ($tag) {
                $dataList[$key]['done'] = $friendM->where("uid={$uid} and status=1 and fid=" . $val['uid'])->getfield('status') ? 1 : 0;       //我是否关注了他
                $articleWhere = "uid={$val['uid']}";
                $followWhere = "fid={$val['uid']} and status=1";
                $concernWhere = "uid={$val['uid']} and status=1";

            } else {
                $dataList[$key]['done'] = 1;
                $articleWhere = "uid={$val['fid']}";
                $followWhere = "fid={$val['fid']} and status=1";
                $concernWhere = "uid={$val['fid']} and status=1";
            }
            $dataList[$key]['articleNum'] = M('article', null, DB_MAIN_CFG)->where($articleWhere)->count();
            $dataList[$key]['followNum'] = $friendM->where($followWhere)->count();
            $dataList[$key]['concernNum'] = $friendM->where($concernWhere)->count();
        }
        
        //获取昵称
        if($tag){
            $userData= $friendM->join("left join u_user_base as jo on jo.uid = friendship.uid")->where("friendship.fid={$uid} and status=1")->limit($offset,$pagesize)->select();
        }else{
            $userData= $friendM->join("left join u_user_base as jo on jo.uid = friendship.fid")->where("friendship.uid={$uid} and status=1")->limit($offset,$pagesize)->select();
        }
        foreach($userData as $key => $val){
            $dataList[$key]['nickname']=$val['nickname'];
        }


        $dataSuccess['total'] = count($dataList);         //获取数据总条数
        $dataSuccess['totalPage'] = ceil($dataSuccess['total']/$pagesize);  //总页数
        $dataSuccess['nextPage'] = $dataSuccess['page'] >= $dataSuccess['totalPage']?0:1;    //是否还有下一页

        $dataSuccess['data']=$dataList;
//        S($shipcache,$dataSuccess,5*60*1000);
        return $dataSuccess;
    }

    /**
     * description:我是否关注了好友的关注和粉丝
     * @param $uid 关注操作的人
     * @param $fid 被关注的人
     * return array
     */
    public static function followStatus($uid,$fid){
        $dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
        $dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');

        if(!$uid){
            $dataFailure['msg']="请先登录后操作";
            return $dataFailure;
        }
        $statusM = M("friendship",null,DB_MAIN_CFG);
        foreach($fid as $val){
            $status[] = $statusM->where("uid={$uid} and status=1 and fid=".$val)->find()?1:0;
        }
        $dataSuccess['done'] = $status;
        return $dataSuccess;
    }


}